package com.chenyaowei.happyhub.controller;

import com.chenyaowei.happyhub.mapper.AdminMapper;
import com.chenyaowei.happyhub.mapper.IndexNavMapper;
import com.chenyaowei.happyhub.pojo.Admin;
import com.chenyaowei.happyhub.pojo.IndexNav;
import com.chenyaowei.happyhub.service.RegisterService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/adminManger")
public class AdminAccountController {
    @Resource
    IndexNavMapper indexNavMapper;
    @Resource
    AdminMapper adminMapper;
    @Resource
    RegisterService registerService;

    @RequestMapping("/showAdmin")
    String showAdmin(Model model) {
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        List<Admin> adminList = adminMapper.selectAllAdmin();
        model.addAttribute("indexNavList", indexNavList);
        model.addAttribute("adminList", adminList);
        return "admin";
    }

    @RequestMapping("/deleteAdmin")
    String deleteAdmin(Model model, String adminId) {
        if (!adminId.equals("1")) {
            adminMapper.deleteAdmin(adminId);
        }
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        List<Admin> adminList = adminMapper.selectAllAdmin();
        model.addAttribute("indexNavList", indexNavList);
        model.addAttribute("adminList", adminList);
        return "admin";
    }

    @RequestMapping("/toInsertAdmin")
    String toInsertAdmin(Model model) {
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        model.addAttribute("indexNavList", indexNavList);
        return "insertAdmin";
    }

    @RequestMapping("/insertAdmin")
    String insertAdmin(Model model, String username, String password, String email, String role, String confirmPassword) {
        registerService.setParameters(username, password, confirmPassword, email, model);
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        model.addAttribute("indexNavList", indexNavList);
        if (registerService.register()) {
            adminMapper.insertAdmin(username, password, email, role);
            List<Admin> adminList = adminMapper.selectAllAdmin();
            model.addAttribute("adminList", adminList);
            return "admin";
        } else
            return "insertAdmin";
    }

    @RequestMapping("/updateAdmin")
    String updateAdmin(Model model, String adminId, String username, String password, String email, String role) {
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        model.addAttribute("indexNavList", indexNavList);
        List<Admin> users=adminMapper.selectAdminListByUsername(username);
        if ( users.size()<2) {
            if (adminId.equals("1"))
                role="ROLE_ADMIN";
            adminMapper.updateAdmin(adminId, username, password, email, role);
        }
        List<Admin> adminList = adminMapper.selectAllAdmin();
        model.addAttribute("adminList", adminList);
        return "admin";
    }

}
